package Lab15;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


//解决两个客户中的航班数据不同步的问题
public class CustomerFuncImpl implements CustomerFunc{

    //获取单例
    List<CustomerInfo> customerInfos = CustomerSigleList.getCustomerInfos();
    PlaneFunc planeFunc = new PlaneFuncImpl();
    @Override
    public void addCustomerInfo() {
        CustomerInfo customerInfo = new CustomerInfo();
        System.out.println("录入客户信息：");
        System.out.println("客户姓名：");
        Scanner sc = new Scanner(System.in);
        String name = sc.next();
        customerInfo.setName(name);
        System.out.println("客户证件号：");
        String Id = sc.next();
        customerInfo.setId(Id);
        System.out.println("客户联系方式：");
        String phone = sc.next();
        customerInfo.setPhone(phone);
        System.out.println("当前所有航班信息：");
        if(PlaneInfoSigleList.getPlaneInfos() == null || PlaneInfoSigleList.getPlaneInfos().size() == 0){
            System.out.println("没有航班信息！无法无法操作！");
            return;
        }
        //显示航班信息
        planeFunc.showPlaneInfo(PlaneInfoSigleList.getPlaneInfos());
        //订票， 为该客户添加1条航班信息
        System.out.println("请选择航班编号：");
        String planeId = sc.next();
        PlaneInfo p = addTicket(planeId);
        if(p == null){
            System.out.println("没有该航班！订票失败！");
            return;
        }
        customerInfo.getPlaneInfos().add(p);
        //为客户添加列表至客户数据库
        customerInfos.add(customerInfo);
        System.out.println("客户姓名\t证件号\t\t联系方式\t\t航班信息");
        System.out.println(customerInfo);
        System.out.println("添加成功！");
    }

    @Override
    public void searchCustomerInfo() {
        List<CustomerInfo> customerInfoSearch = new ArrayList<>();
        do {
            System.out.println("查询客户信息：");
            System.out.println("请输入客户证件号：");
            Scanner sc = new Scanner(System.in);
            String id = sc.next();
            customerInfoSearch = searchById(id);
            if (customerInfoSearch.size() != 0) {
                showCustomerInfo(customerInfoSearch);
                break;
            }
            System.out.println("没有该客户信息，请重新输入：");
        } while (customerInfoSearch.size() == 0);
    }

    //查询客户信息
    private List<CustomerInfo> searchById(String id) {
        List<CustomerInfo> customerWay = new ArrayList<>();
        //遍历客户数据库
        for (CustomerInfo customerInfo : customerInfos) {
            if (customerInfo.getId().equals(id)) {
                customerWay.add(customerInfo);
            }
        }
        return customerWay;
    }
    @Override//修改客户信息--->订票，退票，改签
    public void updateCustomerInfo() {
        PlaneFunc planeFunc = new PlaneFuncImpl();
        //id是客户唯一标识符,根据id查询
        System.out.println("修改客户信息：");
        System.out.println("请输入客户证件号：");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();
        List<CustomerInfo> customerInfoList = searchById(id);
        if(customerInfoList == null || customerInfoList.size() == 0){
            System.out.println("没有该客户信息！");
            return;
        }
        if(customerInfoList != null && customerInfoList.size() != 0){
            //显示查询的客户信息
            CustomerInfo customerInfo = customerInfoList.get(0);
            System.out.println("客户姓名\t证件号\t\t联系方式\t\t航班信息");
            System.out.println(customerInfo);//显示当前客户信息
            System.out.println("当前所有航班信息：");
            if(PlaneInfoSigleList.getPlaneInfos() == null || PlaneInfoSigleList.getPlaneInfos().size() == 0){
                System.out.println("没有航班信息！无法操作！");
                return;
            }
            planeFunc.showPlaneInfo(PlaneInfoSigleList.getPlaneInfos());
            //另一种方法，调用先打印客户信息，在调用showCustomerInfo方法打印该客户航班信息
            System.out.println("请选择修改内容：");
            System.out.println("**            1.订票");
            System.out.println("**            2.退票");
            System.out.println("**            3.改签");
            int choice = sc.nextInt();
            System.out.println("请输入航班Id：");
            String planeId = sc.next();
            switch (choice) {
                case 1:
                    PlaneInfo p = addTicket(planeId);
                    if(p == null){
                        System.out.println("没有该航班！订票失败！");
                        return;
                    }
                    //添加航班信息
                    customerInfo.getPlaneInfos().add(p);
                    System.out.println("订票成功！");
                    break;
                case 2:
                    // 删除航班信息（是否存在航班，航班票+1，用用户信息中去除这一条航班信息）
                    deleteTicket(planeId, customerInfo.getPlaneInfos());
                    break;
                case 3:
                    //修改航班信息(退票+订票)
                    deleteTicket(planeId, customerInfo.getPlaneInfos());
                    System.out.println("请选择新的航班编号：");
                    String Id = sc.next();
                    PlaneInfo TD = addTicket(Id);
                    if(TD == null){
                        System.out.println("没有该航班！改签失败！");
                        return;
                    }
                    customerInfo.getPlaneInfos().add(TD);
                    System.out.println("改签成功！");
                    break;
                default:
                    System.out.println("输入错误！");
            }
        }
    }
    //订票//订票，（查看是否有航班，航班是否有座位）
    private PlaneInfo addTicket(String planeId) {
        PlaneInfo addTicket = new PlaneInfo();
        PlaneInfo planeInfo = searchPlaneInfoById(planeId);//航班数据库插上来的
        if(planeInfo == null){
            System.out.println("没有该航班！");
            return null;
        }
        System.out.println("航班编号\t航线\t\t\t时间\t\t座位数");
        System.out.println(planeInfo);
        if(planeInfo.getSeatNum() <= 0){
            System.out.println("该航班已无座位！");
            return null;
        }
        //减去座位数
        planeInfo.setSeatNum(planeInfo.getSeatNum()-1);
        refreshCustomerInfo();//同步
        return planeInfo;
    }

    //在客户信息的航班数据库中查找
    private PlaneInfo searchPlaneInfoByIdInCustomer(String planeId,List<PlaneInfo> list){
        PlaneInfo searchId = new PlaneInfo();
        for (PlaneInfo planeInfo : list) {
            if (planeInfo.getPlaneId().equals(planeId)) {
                return planeInfo;
            }
        }
        return null;
    }

    //在航班数据库中查找
    private PlaneInfo searchPlaneInfoById(String planeId){
//        PlaneInfo searchId = new PlaneInfo();
        for (PlaneInfo planeInfo : PlaneInfoSigleList.getPlaneInfos()) {
            if (planeInfo.getPlaneId().equals(planeId)) {
                return planeInfo;
            }
        }
        return null;
    }

    private void deleteTicket(String planeId,List<PlaneInfo> list) {
        PlaneInfo p = searchPlaneInfoByIdInCustomer(planeId,list);
        if(p == null){
            System.out.println("客户没有该航班！无法退票！");
            return;
        }
//        p.setSeatNum(p.getSeatNum()+1);//座位数+1
        list.remove(p);
        p = searchPlaneInfoById(planeId);//修改的是航班数据库中的航班信息
        p.setSeatNum(p.getSeatNum()+1);
        refreshCustomerInfo();//同步
        System.out.println("退票成功！");
    }

    @Override
    //另一种方法，调用先打印客户信息，在调用showCustomerInfo方法打印该客户航班信息
    public void showCustomerInfo(List<CustomerInfo> customerInfoTemps) {
        System.out.println("当前客户信息：");
        System.out.println("客户姓名\t证件号\t\t联系方式\t\t航班信息");
        for (CustomerInfo customerInfo : customerInfoTemps) {
            System.out.println(customerInfo);
        }
        System.out.println();
    }

    ////解决两个客户中的航班数据不同步的问题
    //刷新客户信息，每一次客户的航班信息都来自航班数据库//注意到，id是唯一标识的，所以可以利用id进行同步
    private void refreshCustomerInfo() {//在每次有票数修改的地方调用
        for (CustomerInfo customerInfo : customerInfos) {//遍历客户数据库
            List<PlaneInfo> cusPlaneInfos = customerInfo.getPlaneInfos();//获取每个客户航班信息列表
            for (PlaneInfo cusPlaneInfo : cusPlaneInfos) {
                PlaneInfo planeInfo = searchPlaneInfoById(cusPlaneInfo.getPlaneId());
                cusPlaneInfo.setSeatNum(planeInfo.getSeatNum());
            }
        }
    }
}